<template>
  <!--   :close-on-click-modal="false" 取消点击背景默认关闭弹窗 -->
  <el-dialog
    :title="showTitle"
    :visible="showDialog"
    :close-on-click-modal="false"
    @close="handleClose"
    @open="getSimpleList"
  >
    <!-- label 表单域的标题 -->
    <el-form ref="addForm" label-width="120px" :model="form" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
          <!-- 下拉value一般是id  一把看后端需要什么字段-->
          <el-option
            v-for="item in employeesList"
            :key="item.id"
            :label="item.username"
            :value="item.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="handleClose">取 消</el-button>
      <el-button type="primary" @click="submitDepts">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getSimpleUserList } from '@/api/emploess'
import { addDepartmentApi, getEditDepartmentApi, EditDepartmentApi } from '@/api/department'
export default {
  name: 'AddDepts',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    nodeData: {
      type: Object,
      required: true
    },
    departList: {
      type: [Object, Array],
      required: true
    }
  },
  data() {
    return {
      form: {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      },
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: ['change', 'blur'] },
          { min: 1, max: 10, message: '部门名称字符长度为1-10', trigger: ['change', 'blur'] },
          { validator: this.validatorDeptName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: ['change', 'blur'] },
          { min: 1, max: 10, message: '部门编码长度为1-10', trigger: ['change', 'blur'] },
          { validator: this.validatorDeptCodeRepeat, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '输入不能为空', trigger: ['change', 'blur'] }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: ['change', 'blur'] },
          { min: 1, max: 300, message: '部门介绍长度为1-300', trigger: ['change', 'blur'] }
        ]
      },
      employeesList: []

    }
  },
  computed: {
    showTitle() {
      return this.form.id ? '修改部门' : '添加子部门'
    }
  },
  methods: {
    handleClose(done) {
      // this.$emit('closeDialogFn', false)
      // 父组件使用了.sync修饰符 可以让子组件修改props的值
      // this.showDialog = false
      // 但是子组件必须通过固定语法去修改父组件的数据
      // 语法: this.$emit('update:props传过来的值',你要修改的值)
      this.$emit('update:showDialog', false)
      // 重置表单
      // 重置表单只会重置双向绑定的数据
      this.$refs.addForm.resetFields()
      this.form = {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      }
    },
    validatorDeptName(rule, value, callback) {
      let children = []
      if (this.form.id) {
        // 修改的逻辑
        // 先判断 输入的内容是否和我原来的内容一样
        if (value === this.nodeData.name) {
          callback()
          return
        }
        // 如果输入的内容不一样 则比较输入内容和 其他兄弟节点
        children = this.departList.filter(item => item.pid === this.nodeData.pid) || []
      } else {
        // 添加的逻辑
        // 校验 判断当前部门子部门 是否和你输入的内容一样
        children = this.departList.filter(item => item.pid === this.nodeData.id)
        console.log('children', children)
        // console.log(this.departList)
        // 只要子部门和值相同就不通过
      }
      children.some(item => item.name === value) ? callback(new Error('当前部门已经存在')) : callback()
    },
    validatorDeptCodeRepeat(rule, value, callback) {
      // 修改的时候输入的部门code和我自己一样 那么通过
      if (this.form.id) {
        // 修改操作
        if (this.nodeData.code === value) {
          callback()
          return
        }
      }
      // 校验你输入的内容 和列表中的任何一条数据编码 如果重复不通过
      this.departList.some(item => item.code === value) ? callback(new Error('部门编码不能重复')) : callback()
    },
    async getSimpleList() {
      const { data } = await getSimpleUserList()
      console.log('获取员工成功', data)
      this.employeesList = data
    },
    submitDepts() {
      this.$refs.addForm.validate(async flag => {
        console.log(flag)
        if (!flag) return

        if (this.form.id) {
          const res = await EditDepartmentApi(this.form)
          console.log('修改成功', res)
          // 提示消息
          this.$message.success('修改成功')
        } else {
          const res = await addDepartmentApi({ ...this.form, pid: this.nodeData.id })
          console.log('添加成功', res)
          this.$message.success('添加成功')
        }
        // 关闭弹框
        this.handleClose()
        // 刷新数据
        this.$emit('add-depts-success')
      })
    },
    async showEditDepartmentApi() {
      // 这个事件不能写在open事件里 因为open事件一打开就会立即修改
      const { data } = await getEditDepartmentApi(this.nodeData.id)
      console.log('获取部门详情成功', data)
      this.form = data
    }
  }
}
</script>

<style scoped lang="scss">

    ::v-deep .el-dialog__header {
    background: #66b1ff;

    .el-icon-close:before {
      color: #fff;
    }

    .el-dialog__title {
      color: #fff;
    }

  }

</style>
